const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet,HttpPost, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/miniapp/user')
class UserController extends Controller {

    //首页轮播图
    @HttpGet('/get/carouselimg')
    @ExceptionHandler()
    async getCarouselimg() {
        this.result.data = await this.service.miniapp.user.main.getCarouselimg();
        return '查询成功'
    }

    //获取我的设备
    @HttpGet('/get/device/:user_id')
    @ExceptionHandler()
    async getDevice() {
        const rule = {
            user_id: 'int'
        };
        this.validate(rule, this.ctx.params);
        this.result.data = await this.service.miniapp.user.main.getDevice(this.ctx.params.user_id);
        return '查询成功'
    }

    //获取个人中心信息
    @HttpGet('/info/:open_id')
    @ExceptionHandler()
    async getInfo() {
        const rule = {
            open_id: 'string'
        };
        this.validate(rule,this.ctx.params);
        this.result.data = await this.service.miniapp.user.main.getInfo(this.ctx.params.open_id);
        return '查询成功'
    }



    //绑定信息
    @HttpPost('/bind')
    @ExceptionHandler()
    async bind() {
        const rule = {
            wx_id: 'string',
            user_number: 'int',
            wx_name: 'string',
            wx_pic: 'string',
            phone: 'string',
            open_id: 'string',
            type: 'int'
        };
        this.validate(rule,this.ctx.request.body);
        this.result.data = await this.service.miniapp.user.main.bind(this.ctx.request.body);
        return '查询成功'
    }

    //搜索设备根据belong
    @HttpGet('/search')
    @ExceptionHandler()
    async search() {
        const rule = {
            belong_number: 'int',
            name: 'string'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.user.main.search(this.ctx.request.query.belong_number,this.ctx.request.query.name);
        return '查询成功'
    }

    @HttpGet('/getall')
    @ExceptionHandler()
    async getall() {
        const rule = {
            belong_number: 'int'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.user.main.getall(this.ctx.request.query.belong_number);
        return '查询成功'
    }

    //用户更改个人信息
    @HttpPost('/change/info')
    @ExceptionHandler()
    async changeInfo() {
        const rule = {
            open_id: 'string',
            wx_name: 'string',
            wx_pic: 'string',
            sex: 'int'
        };
        this.validate(rule,this.ctx.request.query);
        this.result.data = await this.service.miniapp.user.main.changeInfo(this.ctx.request.query);
        return '修改成功'
    }
}

module.exports = UserController;
